package com.cn.demo.service;

import com.cn.demo.config.SeckillQueueFactory;
import com.cn.demo.entity.Activity;
import com.cn.demo.entity.SeckillGoods;
import com.cn.demo.mapper.ActivityMapper;
import org.redisson.api.RQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动
 *
 * @author wangjf
 * @version 1.0
 * @createDate 2019/11/18 14:28
 * @projectName test
 */
@Service
public class ActivityService extends BaseService<ActivityMapper, Activity> {

    @Autowired
    private SeckillQueueFactory seckillQueueFactory;

    @Transactional(rollbackFor = Exception.class)
    public boolean saveActivity(Activity activity) {

        RQueue<SeckillGoods> queue = seckillQueueFactory.getQueue();
        String queueId = queue.getName();
        activity.setQueueId(queueId);
        save(activity);

        for (int i = 0; i < activity.getAmount(); i++) {
            SeckillGoods seckillGoods = new SeckillGoods();
            seckillGoods.setId(System.currentTimeMillis() + "_" + i);
            seckillGoods.setGoodsId(activity.getGoodsId());
            seckillGoods.setQueueId(queueId);
            queue.add(seckillGoods);
        }

        return Boolean.TRUE;
    }

    /**
     * 启动活动
     *
     * @param id
     * @return boolean
     * @author: wangjf
     * @createTime: 2019/11/21 15:13
     */
    public boolean startActivity(Long id) {

        Activity byId = getById(id);
        if (byId == null) {
            return Boolean.FALSE;
        }

        if (byId.getStatus() != 0) {
            return Boolean.FALSE;
        }

        Activity updateObj = new Activity();
        updateObj.setId(byId.getId());
        updateObj.setStatus(1);
        updateById(updateObj);
        seckillQueueFactory.startQueue(byId.getQueueId());

        return Boolean.TRUE;
    }

    /**
     * 结束活动
     *
     * @param id
     * @return boolean
     * @author: wangjf
     * @createTime: 2019/11/21 15:15
     */
    public boolean endActivity(Long id) {

        Activity byId = getById(id);
        if (byId == null) {
            return Boolean.FALSE;
        }

        if (byId.getStatus() != 1) {
            return Boolean.FALSE;
        }
        Activity updateObj = new Activity();
        updateObj.setId(byId.getId());
        updateObj.setStatus(2);
        updateById(updateObj);
        seckillQueueFactory.endQueue(byId.getQueueId());
        return Boolean.TRUE;
    }

    /**
     * 取消活动
     *
     * @param id
     * @return boolean
     * @author: wangjf
     * @createTime: 2019/11/21 15:16
     */
    public boolean cancelActivity(Long id) {

        Activity byId = getById(id);
        if (byId == null) {
            return Boolean.FALSE;
        }

        if (byId.getStatus() != 0) {
            return Boolean.FALSE;
        }
        Activity updateObj = new Activity();
        updateObj.setId(byId.getId());
        updateObj.setStatus(-1);
        updateById(updateObj);
        seckillQueueFactory.cancelQueue(byId.getQueueId());
        return Boolean.TRUE;
    }


}
